/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package downloadmanager;

import java.io.Serializable;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JProgressBar;

/**
 *
 * @author MX
 */
public class DownloadsModel extends javax.swing.table.AbstractTableModel implements Serializable, Observer
{
    private static final long serialVersionUID = 2L;
    private int _id = 0;
    private ArrayList<Download> _data = new ArrayList<Download>();
    private String _headers[] = new String[] { "Filename", "Progresso", "Done", "Size", "Estado" };
    private Class  _columns[] = new Class[] { String.class, JProgressBar.class, String.class, String.class, String.class };

    public DownloadsModel() {
    }
    
    public void init() {
        for (Download d : getAll())
        {
            // Download init operations
            d.init();

            // Add Observation
            d.addObserver(this);
        }
    }
    
    public Download create(String source) throws MalformedURLException {
        return new Download(_id++, source, App.getApplication().getPreferencesModel().get("defaultFolder"));
    }
    
    public void add(Download f) {
        // Observate Download
        f.addObserver(this);
        
        // Add to container
        _data.add(f);
        
        // If not scheduled, start downloading
        if (! f.isScheduled())
        {
            f.download();
        }

        // Update Model (notify observers)
        fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
}
    
    public void update(Observable o, Object arg) {
        int index = _data.indexOf(o);
        fireTableRowsUpdated(index, index);
    }

    public int getIndex(Object o) {
        return _data.indexOf(o);
    }
    
    public Download get(int i) {
        try
        {
            return _data.get(i);
        }
        catch (IndexOutOfBoundsException e)
        {
            return null;
        }
    }
    
    public List<Download> getAll() {
        return _data.subList(0, _data.size());
    }
    
    public void delete(int row)
    {
        // Stop download, if its active!
        _data.get(row).stop();

        // Remove from the ArrayList
        _data.remove(row);

        // Fire events
        fireTableRowsDeleted(row, row);
    }
    
    public void stopActive()
    {
        for (Download f : _data)
        {
            if (f.getTheState() == Download.State.ACTIVE ||
                f.getTheState() == Download.State.BEGIN ||
                f.getTheState() == Download.State.CONNECTING)
            {
                f.stop();
            }
        }
    }
    
    @Override public String getColumnName(int columnIndex) {
        return _headers[columnIndex];
    }

    public int getRowCount() {
        return _data.size();
    }

    public int getColumnCount() {
        return _columns.length;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        Download f = _data.get(rowIndex);
        
        switch (columnIndex)
        {
            case 0: return f.getFilename();
            case 1: return f.getProgress();
            case 2: return f.getDone() == 0  ? "" : f.formattedBytes(f.getDone());
            case 3: return f.getSize() == -1 ? "" : f.formattedBytes(f.getSize());
            case 4: return f.getState();
            case 5: return f.getId();
        }
        
        return null;
    }
}
